package com.generalbackend.role.controller;

import com.generalbackend.common.annotion.ApiPermission;
import com.generalbackend.common.enums.MenuEnum;
import com.generalbackend.common.model.CommonResult;
import com.generalbackend.role.model.entity.Permission;
import com.generalbackend.role.model.query.PermissionQuery;
import com.generalbackend.role.service.IPermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
* @Description 菜单权限表控制层
* @author tim.xie
* @date 2021-12-10 11:01
*/
@RestController
@RequestMapping(value = "/api/permission")
public class PermissionController {

    @Autowired
    private IPermissionService  permissionService;


    @RequestMapping(value = "/list" ,method = RequestMethod.POST)
    public CommonResult list(@RequestBody PermissionQuery permissionQueryOption ){
           return  CommonResult.successCommonResult(permissionService.list(permissionQueryOption),"查询列表成功");
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public CommonResult add(@RequestBody Permission permission){
        permissionService.insert(permission);
        return CommonResult.successCommonResult("新增菜单权限表成功");
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public CommonResult update(@RequestBody Permission permission){
        permissionService.update(permission);
        return CommonResult.successCommonResult("修改菜单权限表成功");
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.DELETE)
    public CommonResult deleteById(@RequestParam("id")String id){
        permissionService.deleteById(id);
        return CommonResult.successCommonResult("删除菜单权限表成功");
    }

    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public CommonResult getById( @RequestParam("id")String id ){
        return CommonResult.successCommonResult(permissionService.selectById(id),"查询成功");
    }

    @ApiOperation(httpMethod = "GET", value = "获取菜单树")
    @ApiPermission(name = "获取菜单树", isAll = true)
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public CommonResult getPermissionTree() {
        return CommonResult.successCommonResult(permissionService.getPermissionTree(), "获取菜单树成功");
    }

    @ApiOperation(httpMethod = "GET", value = "获取除了权限管理外的所有菜单树")
    @ApiPermission(name = "获取除了权限管理外的所有菜单树", menu = {MenuEnum.RoleManage})
    @RequestMapping(value = "/allTree", method = RequestMethod.GET)
    public CommonResult getAllPermissionTree() {
        return CommonResult.successCommonResult(permissionService.getAllPermissionTree(), "获取成功");
    }

}
